* This code replicates the main results in Tables 1B, 2B, 3B, 6B 
* Product Innovation and Credit Market Disruptions (forthcoming RFS)
* by Joao Granja and Sara Moreira
*
* The data set is mainDSfirmsample_placebo.dta
* This is a pseudo data set that illustrates the format of the original data set that combines firm level information from LPC Dealscan and RMS Nielsen.

clear all
set more off

use "mainDSfirmsample_placebo.dta", clear
tsset firm year

// Step 2) Defining Crisis Years
gen crisis = year == 2008 | year == 2009 | year == 2010

// Step 3) Defining Fixed Effects and Control Variables
egen st2 = group(st)
gen lnrev = ln(1+rev_it)
gen laglnrev = l.lnrev
gen lagprod = ln(l.products_it)
gen lagavrev = ln(l.rev_mean_it)
xtile rev = l.rev_it, nq(10)
xtile nprod = l.T2_it, nq(10)

// Label Variables
label var share_exp_short "Firm Credit Shock"
label var crisis "I(Crisis)"
label var laglnrev "Ln(Firm Revenue)"
label var hhi_product_it "Rev. Concentration Index"
label var lagprod "Ln(Nbr. Products)"
label var lagavrev "Ln(Rev. per Product)"
label var N_it "Nbr. of New Products (All)"
label var Na_it "Nbr. of New Products (Old)"
label var Nf_it "Nbr. of New Products (New)"
label var product_intro "I(New Prod. =1) (All)"
label var product_intro_a "I(New Prod. =1) (Old)"
label var product_intro_b "I(New Prod. =1) (New)"
label var T2_it "Total Nbr. of Products"
label var modules_it "Total Nbr. of Product Mod."
label var groups_it "Total Nbr. of Product Groups"
label var rev_r_it "Total Annual Revenues"

global a laglnrev hhi_product_it lagavrev
winsor2 entryrate entryrate_a entryrate_b $a, replace cuts(1 99)

********************************************************************************
// Table 1: Panel B: Summary Statistics Full Sample
********************************************************************************

eststo: estpost tabstat share_exp_short N_it Na_it Nf_it product_intro product_intro_a product_intro_b T2_it modules_it groups_it rev_r_it if entryrate!=. & share_exp_short!=., s(N mean sd q) c(stat)
*...to latex...
esttab using "Table1B.tex", cells("count(fmt(a0)) mean(fmt(a3)) sd(fmt(a3)) p25(fmt(a3)) p50(fmt(a3)) p75(fmt(a3)) ") /// 
label nodepvars nomtitles noobs plain lines fragment drop() noomit width(\hsize) replace
eststo clear

********************************************************************************
// Table 2: Panel B: Product Innovation: Evolution over Time
********************************************************************************

preserve
drop if year==2006
winsor2 entryrate entryrate_a entryrate_b, replace cuts(1 99)
collapse entryrate entryrate_a entryrate_b product_intro product_intro_a product_intro_b, by(year)
tsset year

mkmat _all, matrix(b)
outtable using "Table2B.tex", mat(b) replace nobox center f(%9.0f %12.3fc %12.3fc %12.3fc %12.3fc %12.3fc %12.3fc) norowlab
restore

********************************************************************************
// Table 3 Panel B: Product Innovation and Credit-Market Disruption (Individual Firm Variation)
********************************************************************************

eststo: reghdfe entryrate c.share_exp_short#c.crisis , absorb(firm year  nprod) vce(cl st2 )
eststo: reghdfe entryrate c.share_exp_short#c.crisis $a, absorb(firm year nprod rev) vce(cl st2)
eststo: reghdfe entryrate_a c.share_exp_short#c.crisis , absorb(firm year nprod) vce(cl st2)
eststo: reghdfe entryrate_a c.share_exp_short#c.crisis $a, absorb(firm year nprod rev) vce(cl st2)
eststo: reghdfe entryrate_b c.share_exp_short#c.crisis  , absorb(firm year nprod) vce(cl st2)
eststo: reghdfe entryrate_b c.share_exp_short#c.crisis $a, absorb(firm year nprod rev) vce(cl  st2)
  
esttab using "Table3B.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear


********************************************************************************
// Table 6 Panel B: Credit-Market Disruption and Novelty of Product Innovation
********************************************************************************

preserve
foreach var of varlist newness_index_imt newness_indexC_imt HN_weightedA{
winsor2 `var' laglnrev  hhi_product_it lagavrev, replace cuts(0 99)
gen ln`var' = ln(1+`var')
eststo: reghdfe ln`var' c.share_exp_short#c.crisis if lagavrev!=., absorb(firm year nprod) vce(cl st2)
eststo: reghdfe ln`var' c.share_exp_short#c.crisis laglnrev  hhi_product_it lagavrev, absorb(firm year nprod rev) vce(cl st2)
}

*
esttab using "Table6B.tex", ///
nodepvars nomti nonum collabels(none) ///
label b(3) se(3) parentheses star(* 0.10 ** 0.05 *** 0.01) ar2   ///
interaction(" $\times$ ") replace plain lines fragment drop() noomit
eststo clear  



